<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginFrom"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到本系统</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginFrom.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginFrom.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="success"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '../../store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { timePart } from '../../utils/time'
let $router = useRouter()
let $route = useRoute()
let userStore = useUserStore()
let loginFrom = reactive({
  username: 'admin',
  password: 'atguigu123',
})
let loading = ref(false)
let loginForms = ref()
const login = async () => {
  await loginForms.value
    .validate()
    .then(async () => {
      loading.value = true
      try {
        await userStore.login(loginFrom)
        loading.value = false
        // 编程式导航跳转
        let redirect: any = $route.query.redirect
        $router.push({ path: redirect || '/' })
        ElNotification({
          type: 'success',
          message: '登录成功',
          title: `Hi,${timePart()}好`,
        })
      } catch (error) {
        ElNotification({
          type: 'error',
          message: (error as Error).message,
        })
        loading.value = false
      }
    })
    .catch(() => {
      ElNotification({
        type: 'error',
        message: '请输入合法数值',
      })
    })
}
// const validatorUserName = (rule:any,value:any,callback:any)=>{
//     console.log(rule)
//     if(value.lenth>4){
//         callback();
//     }else{
//         callback(new Error('非法'));
//     }
// }
const rules = {
  username: [
    { required: true, min: 4, max: 10, message: '4-10位', trigger: 'change' },
    // {trigger:'change',validator:validatorUserName}
  ],
  password: [
    { required: true, min: 6, max: 10, message: '6<位', trigger: 'blur' },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
